﻿namespace DesktopRibbon
{
    using System.Windows;
    using System.Windows.Documents;
    using DesktopRibbon.Helper;
    using Microsoft.Windows.Controls.Ribbon;
    using System.Collections.Generic;
    using System.IO;
    using Launcher;

    /// <summary>
    /// This is the main desktop ribbon control. It is hosted and displayed in the DesktopRibbonWindow, which is this application's main window.
    /// </summary>
    public partial class DesktopRibbonControl : Ribbon
    {
        #region Constants

        //private const string RIBBON_XML = "ribbon.xml";

        #endregion

        #region Fields

        private IconCache.IconCache iconCache = new IconCache.IconCache();
        private IList<Page> pages;
        private TestAdorner dragDropAdorner;
        private DragDropHelper.DragInfo lastDragDropHit;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes the DesktopRibbonControl.
        /// </summary>
        public DesktopRibbonControl()
        {
            InitializeComponent();

            var tab = new RibbonTab() { Header = "Daily" };
            var group = new RibbonGroup() { Header = "General" };
            Items.Add(tab);
            tab.Items.Add(group);

            var shellLauncher = new Launcher.ShellLauncherItem(iconCache, "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
            group.Items.Add(RibbonElementFactory.Create(shellLauncher));

            shellLauncher = new Launcher.ShellLauncherItem(iconCache, "C:\\Windows\\explorer.exe");
            group.Items.Add(RibbonElementFactory.Create(shellLauncher));

            shellLauncher = new Launcher.ShellLauncherItem(iconCache, "http://google.de/");
            group.Items.Add(RibbonElementFactory.Create(shellLauncher));

            pages = new List<Page>();
            
            Page tab1 = new Page();
            Group group1 = new Group();
            group1.Items.Add(new Launcher.ShellLauncherItem(iconCache, "http://google.de/"));
            group1.Items.Add(new Launcher.ShellLauncherItem(iconCache, "C:\\Windows\\explorer.exe"));

            tab1.Groups.Add(group1);
            pages.Add(tab1);

            writeConfig();
            readConfig();
        }

        #endregion

        #region Methods

        private void readConfig()
        {
            string ribbonFile = App.DataDir() + "\\ribbon.xml";
            if (!File.Exists(ribbonFile))
                return;

            System.Xml.Serialization.XmlSerializer reader =
                new System.Xml.Serialization.XmlSerializer(typeof(List<Page>));
            System.IO.StreamReader file = new System.IO.StreamReader(ribbonFile);
            pages = (List<Page>)reader.Deserialize(file);
        }

        private void writeConfig()
        {
            string ribbonFile = App.DataDir() + "\\ribbon.xml";

            System.Xml.Serialization.XmlSerializer writer = 
                new System.Xml.Serialization.XmlSerializer(typeof(List<Page>));
            System.IO.StreamWriter file = new System.IO.StreamWriter(ribbonFile);
            writer.Serialize(file, pages);
            file.Close();
        }

        private void ShutdownApplication(object sender, System.Windows.RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void Ribbon_DragLeave(object sender, System.Windows.DragEventArgs e)
        {
            SetDragAdornerTarget(null);
        }

        private void UpdateDragAdorner()
        {
            var hit = DragDropHelper.GetRibbonControlUnderCursor(this);
            if (((hit == null) != (lastDragDropHit == null)) || (hit.element != lastDragDropHit.element || hit.corner != lastDragDropHit.corner))
            {
                lastDragDropHit = hit;
                SetDragAdornerTarget(hit);
            }
        }

        private void Ribbon_DragOver(object sender, DragEventArgs e)
        {
            UpdateDragAdorner();
        }

        private void Ribbon_Drop(object sender, DragEventArgs e)
        {
            SetDragAdornerTarget(null);
            var hit = DragDropHelper.GetRibbonControlUnderCursor(this);
            var launcher = DragDropHelper.CreateLauncherItem(iconCache, e);
            if (null != launcher)
            {
                var ribbonElement = RibbonElementFactory.Create(launcher);
                if (null != ribbonElement)
                {
                    RibbonControlHelper.AddAsSibling(hit.element, ribbonElement, hit.corner);
                }
            }

        }

        private void SetDragAdornerTarget(DragDropHelper.DragInfo dragInfo)
        {
            if (null != dragDropAdorner)
            {
                lastDragDropHit.element = null;
                AdornerLayer.GetAdornerLayer(this).Remove(dragDropAdorner);
            }
            if (dragInfo == null || dragInfo.element == null)
            {
                dragDropAdorner = null;
            }
            else
            {
                dragDropAdorner = new TestAdorner(dragInfo.element);
                dragDropAdorner.Corner = dragInfo.corner;
                AdornerLayer.GetAdornerLayer(this).Add(dragDropAdorner);
            }
        }

        #endregion

    }
}
